#pragma once

#include <QObject>
#include <QThread>
#include <QQueue>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <opencv2/opencv.hpp>
#include <mutex>
#include <QFileInfo>

class CSQLData : public QObject
{
    Q_OBJECT
public:
    explicit CSQLData(QObject* parent = nullptr) : QObject(parent), m_stop(false) {}

    ~CSQLData() {
        stopProcessing();
    }

    bool connet2DB(QString dbNmae);

    void addImageToQueue(const cv::Mat& image, const QString& path,
        const QString& timestamp, int eventLevel) {
        std::lock_guard<std::mutex> lock(queueMutex);
        m_imageQueue.enqueue(std::make_tuple(image, path, timestamp, eventLevel));
    }

    void processImages();

    void stopProcessing() {
        m_stop = true;
        emit stopSignal(); // 确保所有事件都得到处理
    }

signals:
    void stopSignal();

private:
    QQueue<std::tuple<cv::Mat, QString, QString, int>> m_imageQueue;
    QDateTime currentDateTimeobject_;
    std::mutex queueMutex;
    bool m_stop;
    QSqlDatabase m_db;
    bool m_db_open = false;
    int m_count = 0;
    QSqlQuery* m_query;
};


class DataProducer : public QObject {
    Q_OBJECT

public:
    explicit DataProducer(CSQLData* processor, QObject* parent = nullptr)
        : QObject(parent), m_processor(processor) {
    }

    void addImageToQueue(const cv::Mat& image, const QString& path,
        const QString& timestamp, int eventLevel) {
        if (!image.empty()) {
            // 添加图像到队列中
            m_processor->addImageToQueue(image, path, timestamp, eventLevel);
        }
    }
private:
    CSQLData* m_processor;
};
